/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doanjava;


import java.sql.*;
import java.util.*;
/**
 *
 * @author Mr_Quay
 */
public class DataProvider {
    private Connection con=null;

    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }
    public void open()
    {
        try
        {
            	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            	con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TINTUC;user=han;password=123456");
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    public void close()
    {
        try{
        this.con.close();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    public ResultSet excutequery(String sql)
    {
        ResultSet rs = null;
        try
        {            
            Statement sm = this.con.createStatement();
            rs = sm.executeQuery(sql);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return rs;
    }
    
    public int excuteupdate(String sql)
    {
        int num = -1;
        try
        {
            Statement sm = this.con.createStatement();
            num = sm.executeUpdate(sql);
        }
                catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        finally{
            this.close();
        }
        return num;
    }   
}
